package com.zhongkun.datahome.web.core.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.zhongkun.datahome.service.config.AliyunOSSProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 阿里云配置
 *
 * @author 作者 mallplus E-mail: 951449465@qq.com
 */
@Configuration
@ConditionalOnClass({AliyunOSSProperties.class})
@EnableConfigurationProperties(AliyunOSSProperties.class)
public class AliyunOSSConfig {

    /**
     * 阿里云文件存储client
     * 只有配置了aliyun.oss.access-key才可以使用
     *
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = "file.aliyun.oss.access-key", matchIfMissing = true)
    public OSS oss(AliyunOSSProperties aliyunOSSProperties) {
        return new OSSClientBuilder().build(
                aliyunOSSProperties.getEndpoint(),
                aliyunOSSProperties.getAccessKeyId(),
                aliyunOSSProperties.getAccessKeySecret()
        );
    }
}
